import {postArticle, getArticleList, delArticle, updateArticleDetail, getAllArticleComment} from '@/api/article'
import {uploadImage} from '@/api/upload'

const state = {
  articleList: [],
  total: 0,
  commentList: []
}

const mutations = {
  SET_ARTICLELIST(state, articleList) {
    state.articleList = articleList;
  },
  SET_TOTAL(state, total) {
    state.total = total;
  },
  SET_COMMENTLIST(state, data) {
    state.commentList = data;
  }
}

const actions = {
  async postNewArticle({commit}, data) {
    let result = await postArticle(data);
    if (result.code === 200) {
      return 'OK'
    }
  },
  async getArticles({commit}, data) {
    let response = await getArticleList(data.society_id, data.pageNo, data.limit)
    if (response.code === 200) {
      commit('SET_ARTICLELIST', response.data.articleList)
      commit('SET_TOTAL', response.data.total)
      return 'OK'
    }
  },
  async uploadImage({commit}, formData) {
    let response = await uploadImage(formData)
    if (response.code === 200) {
      return response.data
    }
  },
  async delArticle({commit}, ids) {
    let result = await delArticle(ids)
    if (result.code === 200) {
      return 'OK'
    }
  },
  async updateArticleDetail({commit}, article) {
    let result = await updateArticleDetail(article.id, article.detail)
    if (result.code === 200) {
      return 'OK'
    }
  },
  async getAllArticleComment({commit}, data) {
    let result = await getAllArticleComment(data);
    commit('SET_COMMENTLIST',result.comments);
    commit('SET_TOTAL',result.total);
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

